package com.funny.stock.user.controller;

import com.funny.stock.base.util.ResultUtil;
import com.funny.stock.base.vo.Result;
import com.funny.stock.user.service.SysUserService;
import com.funny.stock.user.service.UserInfoService;
import com.funny.stock.user.vo.SysUserVO;
import com.funny.stock.user.vo.UserRegisterVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author pengl
 * @date 2020/11/24
 **/
@RestController
public class SysUserController {
    @Autowired
    UserInfoService userInfoService;

    @Autowired
    SysUserService sysUserService;

    @PostMapping("/register")
    public Result<SysUserVO> register(@RequestParam String username,@RequestParam String password){
        if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password)){
            return ResultUtil.error("用户名和密码不能为空");
        }

        if(username.length() < 4 || username.length() > 16){
            return ResultUtil.error("用户名长度必须为4-16位");
        }

        if(password.length() < 6 || password.length() > 20){
            return ResultUtil.error("密码长度必须为6-20位");
        }

        SysUserVO userVO = sysUserService.findByLoginAccount(username);
        if(userVO != null){
            return ResultUtil.error("用户名已存在");
        }

        SysUserVO sysUserVO = userInfoService.registerUser(new UserRegisterVO(username, password));
        sysUserVO.setPassword("");
        return ResultUtil.success(sysUserVO);
    }
}
